# -*- coding: utf-8 -*-
#  /**
#  * Copyright (c) 2022 Beijing Jiaotong University
#  * PhotLab is licensed under [Open Source License].
#  * You can use this software according to the terms and conditions of the [Open Source License].
#  * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
#  *
#  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
#  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
#  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#  *
#  * See the [Open Source License] for more details.
#  */
#  /**
#  * Author: Yang Wang
#  * Created: Oct. 31, 2023
#  * Supported by: National Key Research and Development Program of China
#  */



import logging
import sys

# Create a custom logger
logger = logging.getLogger("phot_engine")
logger.setLevel(logging.DEBUG)    #创建了一个自定义的日志记录器，命名为 "phot"

# Create handlers
#创建了一个日志处理器，这个处理器会将日志消息输出到标准输出（stdout）
custom_handler = logging.StreamHandler(sys.stdout)


class CustomFormatter(logging.Formatter):
    green = '\033[92m'
    grey = '\33[90m'
    yellow = "\x1b[33;20m"
    red = "\x1b[31;20m"
    bold_red = "\x1b[31;1m"
    reset = "\x1b[0m"
    format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"

    FORMATS = {
        logging.DEBUG: green + format + reset,
        logging.INFO: grey + format + reset,
        logging.WARNING: yellow + format + reset,
        logging.ERROR: red + format + reset,
        logging.CRITICAL: bold_red + format + reset
    }

#重写父类 logging.Formatter 中的 format 方法
    def format(self, record):
        log_fmt = self.FORMATS.get(record.levelno)
        formatter = logging.Formatter(log_fmt)
        return formatter.format(record)


# Create formatters and add it to handlers
custom_handler.setFormatter(CustomFormatter())

# Add handlers to the logger
logger.addHandler(custom_handler)